package com.lucky.controller;

import com.lucky.annotation.Authority;
import com.lucky.pojo.vo.User;
import com.lucky.config.SystemConfig;
import com.lucky.contants.WebContant;
import com.lucky.service.UserService;
import com.lucky.util.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @Description
 * @Author Jalyson
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/6/26
 */
@Controller
public class IndexController {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private SystemConfig config;

    @RequestMapping
    public String index() {
        return "index";
    }

    @Value("${server.name}")
    private String serverName;

    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request) {
        String id = request.getSession().getId();
        User authUser = (User) RedisCache.getData(id);
        if (authUser != null) {
            ModelAndView model = new ModelAndView("main");
            Collection<User> all = userService.findAll();
            model.addObject("userLists", all);
            model.addObject(WebContant.USER_AUTH, authUser);
            return model;
        }
        ModelAndView model = new ModelAndView("index");
        return model;
    }
    @Authority(permission = false)
    @RequestMapping("/serverName")
    @ResponseBody
    public String serverName(HttpServletRequest request) {

        return serverName;
    }

    @Authority(permission = false)
    @RequestMapping("/systemConfig")
    @ResponseBody
    public String systemConfig(HttpServletRequest request) {

        return config.getTitle()+"---"+config.getCreateUser()+"---"+config.getCreateTime()+"---"+config.getDetail();
    }
}
